package com.example.multithread;

public class ThreadYieldTest {
    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();
        Thread joinThread = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                try {
                    if (i % 2 == 0) {
                        System.out.println("[Yield]线程礼让执行");
                    }
                    Thread.sleep(100);
                    System.out.println("[" + Thread.currentThread().getName() + "] 子线程执行, i = " + i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "JoinThread");
        joinThread.start();
        for (
                int i = 0;
                i < 1000; i++) {
            Thread.sleep(100);
            System.out.println("[" + Thread.currentThread().getName() + "] 线程执行, i = " + i);
        }
    }
}
